/*
 * DailyQuotationFragment.java
 * business
 *
 * Created by ChenTao on 2017/6/5.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.dailyquotation;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.ui.widget.dialog.ShareDialog;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.DailyQuotationProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.DailyQuotationTitleProvider;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 每日行情
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017-06-05 9:43
 */
public class DailyQuotationFragment extends SuperBaseFragment implements DailyQuotationContract.View {

    private DailyQuotationContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private PtrClassicFrameLayout mPtrFrame;

    private RecyclerView mRecyclerView;

    private MultiTypeLoadMoreAdapter mAdapter;
    private ImageButton mIbtnShare;

    public static DailyQuotationFragment newInstance() {
        return new DailyQuotationFragment();
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mLoadMoreContainer = (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);
        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mIbtnShare = (ImageButton) mRootView.findViewById(R.id.ibtn_share);

        initRecyclerView();
        initListeners();
    }

    private void initRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mAdapter = new MultiTypeLoadMoreAdapter();
        mAdapter.register(String.class, new DailyQuotationTitleProvider());
        mAdapter.register(Goods.class, new DailyQuotationProvider(mActivity));

        mLoadMoreContainer.setRecyclerViewAdapter(mAdapter);
        mLoadMoreContainer.useDefaultFooter();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                mPresenter.loadDailyData(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }
        });

        mLoadMoreContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                // 加载更多
                mPresenter.loadDailyData(false);
            }
        });

        mIbtnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDailyQuotation(mPresenter.getShareEntity());
            }
        });
    }

    @Override
    public void shareDailyQuotation(ShareEntity shareEntity) {
        ShareDialog shareDialog = new ShareDialog(mActivity);
        shareDialog.show();
        shareDialog.setShareLinkData(shareEntity);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_daily_quotation;
    }

    @Override
    public void setPresenter(DailyQuotationContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return DailyQuotationFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void refreshContent(@SuperLoadingLayout.LoadState int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void setItems(List<Object> objects) {
        mAdapter.setItems(objects);
        mAdapter.notifyDataSetChanged();
    }
}
